package br.com.funaction.eventtcontrollerv2.util;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConexaoUtil implements Serializable{

	/**
     * 
     */
    private static final long serialVersionUID = 1L;
    /*** Local Igor ***/
    //private static final String DATABASE = "funactio_eventt_controller";
    //private static final String USER = "root";
    //private static final String PASSWORD = "igoven23";
    /*** Local Igor ***/
    
    /*** Producao ***/
    private static final String DATABASE = "funactio_eventt_controller";
    private static final String USER = "funactio_bd";
    private static final String PASSWORD = "bdeventtcontroller";
    /*** Producao ***/
    
    private static final String STR_DRIVER = "com.mysql.jdbc.Driver";
    private static final String IP = "localhost";
    private static final String STR_CON = "jdbc:mysql://" + IP + ":3306/" + DATABASE;
    
    
    
    public static Connection getConexao() throws SQLException{
    	
    	Connection conn = null;
    	
    	try{
    		Class.forName(STR_DRIVER);
            conn = DriverManager.getConnection(STR_CON, USER, PASSWORD);
            return conn;
    	} catch (ClassNotFoundException e) {
            String errorMsg = "Driver nao encontrado";
            throw new SQLException(errorMsg, e);
        } catch (SQLException e) {
            String errorMsg = "Erro ao obter a conexao";
            throw new SQLException(errorMsg, e);
        }
    }
    
    public static void closeAll(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeAll(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (conn != null || stmt != null) {
                closeAll(conn, stmt);
            }
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeAll(Connection conn, Statement stmt) {
        try {
            if (conn != null) {
                closeAll(conn);
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}